<?php
declare (strict_types=1);

namespace app\listener;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\common\service\GatewayClientServer;
use app\index\service\MessageSendService;

class ConsultingMemberJoined
{

    /**
     * 用户加入聊天 事件监听处理
     *
     * @return mixed
     */
    public function handle(Consulting $consulting)
    {

        if (!is_id($consulting->memberId)) {
            return;
        }

        // 用户加入咨询后

        // 给 用户 发 默认系统消息
        $content = [
            'icon' => ['url' => image_url('/storage/static/images/icon/icon_msg.png')],
            'templates' => [['type' => 'text', 'red' => '102', 'text' => '您的信息已被加密，请放心输入',]],
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

        // 用户加入后，给用户推送咨询列表更新消息
        $data = [
            'action' => CHAT_LIST,
            'data' => []
        ];
        $this->getGatewayClientServer()->sendToUid($consulting->memberId, json_encode($data));

    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

    private function getGatewayClientServer(): GatewayClientServer
    {
        return app(GatewayClientServer::class);
    }
}
